package com.github.microwww.upms.service;

import com.github.microwww.upms.domain.WebappUser;
import com.github.microwww.upms.repository.WebappUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import java.util.Optional;

@Service()
@Transactional()
public class WebappUserService {

    @Autowired()
    private WebappUserRepository webappUserRepository;

    public WebappUserRepository getWebappUserRepository() {
        return webappUserRepository;
    }

    public void setWebappUserRepository(WebappUserRepository webappUserRepository) {
        this.webappUserRepository = webappUserRepository;
    }

    public Page<WebappUser> findAll(int page, int size) {
        return this.webappUserRepository.findAll(PageRequest.of(page, size));
    }

    public Optional<WebappUser> findById(int id) {
        return this.webappUserRepository.findById(id);
    }

    public WebappUser getOrElseThrow(int id) {
        return this.webappUserRepository.findById(id).orElseThrow(() -> {
            return new com.github.microwww.upms.exception.ExistException.NotExist(WebappUser.class);
        });
    }
}
